<template>
    <view>
        <navTabs :tab.sync="tab" :changeTab="_changeTab" :is_round="is_round"></navTabs>
        <slot name="top"></slot>
        <view v-if="tab.cur == nav_id" class="cu-list menu" :class="class_list" v-for="(nav_info, nav_id) in tab.list"
            :key="nav_id">
            <emptyList v-if="!tab.apiList[nav_id] || !tab.apiList[nav_id].list.length" :empty_list_title="empty_list_title || '还没有'+nav_info.title+'记录'"></emptyList>
            <slot v-else name="list" :list="tab.apiList[nav_id].list"></slot>
        </view>
    </view>
</template>

<script>
    export default {
        props: ['tab', 'empty_list_title', 'changeTab', 'is_round', 'class_list'],
        data() {
            return {}
        },
        methods: {
            listInit() {
                Object.keys(this.tab.list).forEach(key => {
                    this.$set(this.tab.apiList, key, {
                        offset: 0,
                        limit: this.tab.list[key].limit || 15,
                        stop: false,
                        list: [],
                    })
                });
                this.getList(this.$api.list_action_page_init);
            },
            listForceInit() {
                this.getList(this.$api.list_action_force_init);
            },
            listAdd() {
                this.getList(this.$api.list_action_add);
            },
            getList(action) {
                this.$api.list(this, {
                    action,
                    data_key: ['tab', 'apiList', this.tab.cur],
                    data_append: this.tab.list[this.tab.cur].data,
                    url: this.tab.list[this.tab.cur].uri,
                    success: (Result, data_key) => {
                        if (this.$api.form.toast(this, Result, data_key)) return;
                        this.$set(this.tab.apiList[this.tab.cur], 'list', this.tab.apiList[this.tab.cur].list
                            .concat(Result.result.list));
                        this.setParentTab();
                    }
                });
            },
            _changeTab(key) {
                if (this.changeTab) {
                    this.changeTab(key);
                }
                this.tab.cur = key;
                this.setParentTab();
                if (this.tab.apiList[key].list.length == 0) {
                    this.getList(this.$api.list_action_init);
                }
            },
            setParentTab() {
                this.$emit('update:tab', this.tab);
            }
        }
    }
</script>
